package com.ruitu.mall_98_seller.ui.home;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.util.AppUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.ui.user.AboutActivity;
import com.ruitu.mall_98_seller.ui.user.LoginActivity;
import com.ruitu.mall_98_seller.ui.user.SecurityActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.Config;
import com.ruitu.router_module.bean.Company;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.User;
import com.ruitu.router_module.util.GlideUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.jpush.android.api.JPushInterface;
import io.rong.imkit.RongIM;

import static com.ruitu.mall_98_seller.ui.user.LoginActivity.PASSWORD_KEY;

public class MineFragment extends BaseFragment {
    private TextView tv_title;//
    private ImageView iv_header;
    private TextView tv_name, tv_com;
    private RelativeLayout rl_1, rl_2, rl_3;
    private TextView tv_quit;// 切换帐号

    public static MineFragment newInstance() {

        Bundle args = new Bundle();

        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Arad.bus.register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetBusMsg(EventModel ev) {
        if (ev.getEventCode() == 19090653) {// 自动登录成功
            setData2View();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mine, container, false);
        tv_title = v.findViewById(R.id.tv_title);
        iv_header = v.findViewById(R.id.iv_header);
        tv_name = v.findViewById(R.id.tv_name);
        tv_com = v.findViewById(R.id.tv_com);
        tv_quit = v.findViewById(R.id.tv_quit);
        rl_1 = v.findViewById(R.id.rl_1);
        rl_2 = v.findViewById(R.id.rl_2);
        rl_3 = v.findViewById(R.id.rl_3);

        ((TextView) v.findViewById(R.id.tv_version)).setText("v" + AppUtils.getAppVersionName());

        tv_title.setText("九八商城企业版");
        setOnClickListener(rl_1, rl_2, rl_3, tv_quit);

        return v;
    }

    @Override
    public void onClick(View v) {
        if (v == rl_1) {// 权限
            startActivity(SecurityActivity.class);
        }
        if (v == rl_2) {// 版本
            hideProgress();
            showProgressWithText(true, "检查新版本");
            Arad.bus.post(new EventModel(19090551));
        }
        if (v == rl_3) {
            startActivity(AboutActivity.class);
        }
        if (v == tv_quit) {// 切换帐号 退出登录
            JPushInterface.setAlias(Arad.app, AppHolder.instance().getSequence(), "1111111111");
            AppHolder.instance().setSequence(0);
            JPushInterface.setAlias(Arad.app, AppHolder.instance().getSequence(), "1111111111");
//            JPushInterface.stopPush(Arad.app);
//            if (JPushInterface.isPushStopped(Arad.app)) {
//                Logg.i("极光推送服务已停止...");
//            } else {
//                Logg.i("极光推送服务运行中...");
//            }
            AppHolder.instance().setUser(null);
            Arad.preferences.putString(PASSWORD_KEY, "").flush();
            RongIM.getInstance().logout();
            Arad.bus.post(new EventModel(19090651));// 获取各种数量
            Arad.bus.post(new EventModel(19091652));// 退出登录

            setData2View();
            startActivity(LoginActivity.class);
        }
    }

    private void setData2View() {
        if (CommonUtils.isLogin()) {
            User user = AppHolder.instance().getUser();
            Company com = user.getCompany();

            tv_title.setText(com.getName());
            tv_name.setText(TextUtils.isEmpty(user.getNickName()) ? user.getUserName() : user.getNickName());
//            tv_com.setText(com.getName());

//            Glide.with(Arad.app).load(Config.BASE_URL + com.getImageUrl())
//                    .apply(Arad.opts_circle).into(iv_header);
            GlideUtil.loadImg(Config.BASE_URL + com.getImageUrl(), iv_header);

            tv_quit.setText("退出登录");
        } else {
            tv_title.setText("九八商城企业版");
            tv_name.setText("未登录");
            tv_quit.setText("登录");
            Glide.with(Arad.app).load(R.mipmap.img_default).into(iv_header);
        }
    }
}
